package com.quanyue.haircut.shop.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.haircut.domain.constant.UserRole;
import com.quanyue.haircut.domain.entity.SlideImage;
import com.quanyue.haircut.shop.service.SlideService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "轮播图")
@RestController
@RequestMapping("/api")
public class SlideControlelr {
    @Autowired
    private SlideService slideService;

    @ApiOperation(value = "添加轮播图")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/slide", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> add(@RequestBody SlideImage image, @ApiIgnore Principal principal) {
        image.setMerchantNo(principal.getMerchantNo());
        slideService.create(image);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改轮播图")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/slide", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> update(@RequestBody SlideImage image) {
        slideService.update(image);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除轮播图")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/slide", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> delete(@RequestParam Integer id) {
        slideService.delete(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "轮播图列表")
    @GetMapping(value = "/slide", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <SlideImage>> find(@ApiIgnore Principal principal) {
        return ServiceResult.success(slideService.findList(principal.getMerchantNo()));
    }
}
